package com.bing.util;

import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import java.awt.*;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2014/6/20.
 */

public class DriverFactory {
    private static final Logger logger = LoggerFactory.getLogger(DriverFactory.class);

    private static ChromeOptions options = new ChromeOptions();
    private static DesiredCapabilities plantomjsCapabilities = new DesiredCapabilities();
    private static DesiredCapabilities firefoxCapabilities = new DesiredCapabilities();
    private static DesiredCapabilities operaCapabilities = new DesiredCapabilities();
    private static DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();

    private static String phantomjs;
    private static String chromePath;

    static {
        logger.info("查找driver............");

        switch (OSinfo.getOSname()) {
            case Windows:
                try {
                    chromePath = ResourceUtils.getFile("classpath:tools/chrome/chromedriver.exe").getAbsolutePath();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                try {
                    phantomjs = ResourceUtils.getFile("classpath:tools/phantomjs-1.9.7-windows/phantomjs.exe").getAbsolutePath();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                break;
            case Linux:
                try {
                    chromePath = ResourceUtils.getFile("classpath:tools/chrome/chromedriver").getAbsolutePath();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                try {
                    phantomjs = ResourceUtils.getFile("classpath:tools/phantomjs-1.9.7-linux-x86_64/bin/phantomjs")
                            .getAbsolutePath();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                break;
            default:
        }


        System.setProperty("webdriver.chrome.driver", chromePath); //设置chrome浏览器驱动路径
        options.addArguments("--disable-plugins-discovery");
        options.addArguments("--disable-save-password-bubble");
        options.addArguments("--disable-images");
     /*   Map<String, Object> contentSettings = new HashMap<String, Object>();
        contentSettings.put("images", 2);
        Map<String, Object> preferences = new HashMap<String, Object>();
        preferences.put("profile.default_content_settings", contentSettings);
        chromeCapabilities.setCapability("chrome.switches", new String[1] {
            "disable-images"
        });*/
        plantomjsCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs);
        plantomjsCapabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
        plantomjsCapabilities.setCapability("phantomjs.page.settings.loadImages", false);

    }

    private DriverFactory() {
        logger.info("实例化driver");
    }

    public static WebDriver getDriver(String browseName) {
        logger.info("初始化webDriver: " + browseName);
        WebDriver driver = null;
        switch (browseName) {
            case "chrome":
                driver = new ChromeDriver(options);
                break;
            case "phantomjs":
            default:
                driver = new PhantomJSDriver(plantomjsCapabilities);
                break;
        }

        int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();

        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
   //     driver.manage().window().setPosition(new Point(-width, 0));
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
        driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);
        return driver;
    }


    public static void setFirefoxPath(String path) {
        firefoxCapabilities.setCapability(FirefoxDriver.BINARY, path);
    }

    public static void setOperaPath(String path) {
        System.setProperty("opera.binary", path);
        operaCapabilities.setCapability("opera.binary", path);
    }
}
